<template>
  <div class="home">
    <!-- 导航栏 -->
    <nav class="navbar">
      <div class="logo">🎬 影院</div>
      <ul class="nav-links">
        <li><router-link to="/">首页</router-link></li>
      </ul>
    </nav>

    <h1 class="page-title">电影列表</h1>
    
    <div v-if="movieStore.loading" class="loading">加载中...</div>
    <div v-else-if="movieStore.error" class="error">{{ movieStore.error }}</div>
    
    <div v-else class="movie-grid">
      <div 
        v-for="movie in movieStore.movies" 
        :key="movie.id" 
        class="movie-card"
        @click="goToMovie(movie.id)"
      >
        <div class="movie-img">
          <img :src="movie.filmImg" :alt="movie.name" />
        </div>
        <div class="movie-info">
          <h3>{{ movie.name }}</h3>
          <p class="english-name">{{ movie.nameEnglish }}</p>
          <p class="cinema">{{ movie.cinema }} - {{ movie.room }}</p>
          <p class="show-time">{{ formatDateTime(movie.showTime) }}</p>
          <p class="price">¥{{ movie.unitPrice }}</p>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { useMovieStore } from '../stores/movie'
import { onMounted } from 'vue'
import { useRouter } from 'vue-router'

export default {
  name: 'HomeView',
  setup() {
    const movieStore = useMovieStore()
    const router = useRouter()

    onMounted(() => {
      movieStore.fetchMovies()
    })

    const goToMovie = (id) => {
      router.push(`/movie/${id}`)
    }

    const formatDateTime = (dateTimeStr) => {
      return new Date(dateTimeStr).toLocaleString('zh-CN')
    }

    return {
      movieStore,
      goToMovie,
      formatDateTime
    }
  }
}
</script>

<style scoped>
.home {
  max-width: 1200px;
  margin: 0 auto;
}

/* 页面标题发光效果 */
.page-title {
  text-align: center;
  margin: 2rem 0;
  color: #f0f0f0;
  font-size: 2.5rem;
  text-shadow: 0 0 8px #ff4d4d, 0 0 12px #ff4d4d;
  border-bottom: 2px solid rgba(255, 77, 77, 0.6);
  display: inline-block;
  padding-bottom: 0.5rem;
}

/* 电影网格 */
.movie-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 2rem;
  padding: 1rem 0;
}

/* 卡片 */
.movie-card {
  background: #1e1e1e;
  border-radius: 10px;
  overflow: hidden;
  box-shadow: 0 4px 15px rgba(0,0,0,0.6);
  transition: transform 0.4s ease, box-shadow 0.4s ease;
  cursor: pointer;
  perspective: 1000px;
}

.movie-card:hover {
  transform: translateY(-10px) scale(1.03) rotateX(3deg) rotateY(-3deg);
  box-shadow: 0 12px 30px rgba(255,77,77,0.3), 0 8px 25px rgba(0,0,0,0.8);
}

.movie-info h3 {
  color: #fff;
}

.english-name,
.cinema,
.show-time {
  color: #bbb;
}

.price {
  color: #ff4d4d;
  font-weight: bold;
  font-size: 1.2rem;
}

/* 海报 */
.movie-img {
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
  background: #000;
}

.movie-img img {
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
}

.movie-info {
  padding: 1.5rem;
}
</style>
